//
//  HBActionSheet.m
//  HBActionSheet
//
//  Created by 韩冰 on 2017/10/23.
//  Copyright © 2017年 韩冰. All rights reserved.
//

#import "HBSheetView.h"
#import "HBCommon.h"

//常量
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenBounds [UIScreen mainScreen].bounds

@interface HBSheetView()
@property (nonatomic, copy)NSString *title;//标题
@property (nonatomic, strong)NSArray *buttonArray;//按钮组
@property (nonatomic, copy)NSString *cancelTitle;//取消按钮
@property (nonatomic, assign)CGFloat actionSheetHeight;
@property (nonatomic, strong)UITapGestureRecognizer *tapGesture;
@property (nonatomic, assign)CGFloat height;

@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UIView *bgView;
@property (nonatomic, strong)UIView *padLine;

@end
@implementation HBSheetView

- (id)initWithTitle:(NSString *)title withButtonArray:(NSArray *)buttonArray withCancleButton:(NSString *)cancelTitle sheetType:(HBSheetType)sheetType handler:(void (^)(NSInteger index))handerAction;{
    

    self = [super init];
    if (self) {
        [self defultSet];
        self.sheetType = sheetType;
        self.type = HBPopViewTypeSheet;
        self.title = title;
        self.buttonArray = buttonArray;
        self.cancelTitle = cancelTitle;
        self.handler = ^(NSInteger tag){
            if (handerAction) {
                handerAction(tag);
            }
        };
        [self setUpUI];
    }
    return self;
}
- (void)defultSet{
    self.titleHeight = 40;
    self.titleColor = [UIColor HB_ColorWithHexString:@"#DEDEDE"];
    self.titleFont = [UIFont systemFontOfSize:12];
    
    self.buttonHeight = 50;
    self.buttonFont = [UIFont systemFontOfSize:14];
    self.buttonTitleColor = [UIColor HB_ColorWithHexString:@"#000000"];
    self.cancleColor = [UIColor blueColor];
    
    self.lineColor = [UIColor HB_ColorWithHexString:@"#E1E1E1"];
    self.lineHeight = 0.5;
    
    self.padColor = [UIColor HB_ColorWithHexString:@"#F2F2F2"];
    self.padHeight = 10;
    self.touchBgWildToHide = YES;
}

- (void)setUpUI{
    if (self.sheetType == HBSheetTypeNormal) {
        [self normalUI];
        
    }else if (self.sheetType == HBSheetTypeNoTitle){
        [self noTitleUI];
    }else if(self.sheetType == HBSheetTypeNoCancel){
        [self noCancleUI];
        
    }else if(self.sheetType == HBSheetTypeNoTitleAndCancel){
        [self noTitleAndCancleUI];
        
    } else {
        
    }
}

- (void)normalUI{
    [self addSubview:self.titleLabel];
    self.titleLabel.text = self.title;
    CGFloat btnY = self.titleHeight;
    for (int  i = 0; i< self.buttonArray.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, btnY, ScreenWidth, self.buttonHeight);
        button.backgroundColor = [UIColor whiteColor];
        [button setTitle: [self.buttonArray objectAtIndexSafely:i] forState:UIControlStateNormal];
        [button setTitleColor:self.buttonTitleColor forState:UIControlStateNormal];
        button.titleLabel.font = self.buttonFont;
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, self.buttonHeight- self.lineHeight, ScreenWidth, self.lineHeight)];
        line.backgroundColor = self.lineColor;
        [button addSubview:line];
        button.tag = 10001+ i;
        btnY = btnY + self.buttonHeight;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    self.padLine.frame = CGRectMake(0, btnY, ScreenWidth, self.padHeight);
    [self addSubview:self.padLine];
    UIButton *cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleButton.frame = CGRectMake(0, btnY + self.padHeight, ScreenWidth, self.buttonHeight);
    [cancleButton setTitle:self.cancelTitle forState:UIControlStateNormal];
    [cancleButton setTitleColor:self.cancleColor forState:UIControlStateNormal];
    [cancleButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cancleButton.titleLabel.font = self.buttonFont;
    cancleButton.backgroundColor = [UIColor whiteColor];
    cancleButton.tag = 10000;
    [self addSubview:cancleButton];
    self.bounds = CGRectMake(0, 0, ScreenWidth, self.buttonHeight *(self.buttonArray.count +1) + self.titleHeight + self.padHeight);
}
- (void)noTitleUI{
   
    CGFloat btnY = 0;
    for (int  i = 0; i< self.buttonArray.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, btnY, ScreenWidth, self.buttonHeight);
        button.backgroundColor = [UIColor whiteColor];
        [button setTitle: [self.buttonArray objectAtIndexSafely:i] forState:UIControlStateNormal];
        [button setTitleColor:self.buttonTitleColor forState:UIControlStateNormal];
        button.titleLabel.font = self.buttonFont;
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, self.buttonHeight- self.lineHeight, ScreenWidth, self.lineHeight)];
        line.backgroundColor = self.lineColor;
        [button addSubview:line];
        button.tag = 10001+ i;
        btnY = btnY + self.buttonHeight;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    self.padLine.frame = CGRectMake(0, btnY, ScreenWidth, self.padHeight);
    [self addSubview:self.padLine];
    UIButton *cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancleButton.frame = CGRectMake(0, btnY + self.padHeight, ScreenWidth, self.buttonHeight);
    [cancleButton setTitle:self.cancelTitle forState:UIControlStateNormal];
    [cancleButton setTitleColor:self.cancleColor forState:UIControlStateNormal];
    [cancleButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    cancleButton.titleLabel.font = self.buttonFont;
    cancleButton.backgroundColor = [UIColor whiteColor];
     cancleButton.tag = 10000;
    [self addSubview:cancleButton];
    self.bounds =  CGRectMake(0, 0, ScreenWidth, self.buttonHeight *(self.buttonArray.count +1)  + self.padHeight);
    
}

- (void)noCancleUI{
    self.titleLabel.text = self.title;
    [self addSubview:self.titleLabel];
    CGFloat btnY = self.titleHeight;
    for (int  i = 0; i< self.buttonArray.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, btnY, ScreenWidth, self.buttonHeight);
        button.backgroundColor = [UIColor whiteColor];
        [button setTitle: [self.buttonArray objectAtIndexSafely:i] forState:UIControlStateNormal];
        [button setTitleColor:self.buttonTitleColor forState:UIControlStateNormal];
        button.titleLabel.font = self.buttonFont;
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, self.buttonHeight- self.lineHeight, ScreenWidth, self.lineHeight)];
        line.backgroundColor = self.lineColor;
        [button addSubview:line];
        button.tag = 10001+ i;
        btnY = btnY + self.buttonHeight;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    self.bounds = CGRectMake(0, 0, ScreenWidth, self.buttonHeight *self.buttonArray.count + self.titleHeight);
    
}
- (void)noTitleAndCancleUI{
    CGFloat btnY = 0;
    for (int  i = 0; i< self.buttonArray.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, btnY, ScreenWidth, self.buttonHeight);
        button.backgroundColor = [UIColor whiteColor];
        [button setTitle: [self.buttonArray objectAtIndexSafely:i] forState:UIControlStateNormal];
        [button setTitleColor:self.buttonTitleColor forState:UIControlStateNormal];
        button.titleLabel.font = self.buttonFont;
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, self.buttonHeight- self.lineHeight, ScreenWidth, self.lineHeight)];
        line.backgroundColor = self.lineColor;
        [button addSubview:line];
        button.tag = 10001+ i;
        btnY = btnY + self.buttonHeight;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    self.bounds = CGRectMake(0, 0, ScreenWidth, self.buttonHeight *self.buttonArray.count);
    
}


- (void)buttonAction:(UIButton *)btn{
    
    if (btn.tag ==1000) {
        [self hide];//取消按键]
    }else{
       NSInteger tag = btn.tag - 10001;
        [self hide];
        if (self.handler) {
            self.handler(tag);
        }
    }
    
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,self.titleHeight)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = self.titleFont;
        _titleLabel.backgroundColor = [UIColor whiteColor];
        _titleLabel.textColor = self.titleColor;
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.titleHeight -self.lineHeight, ScreenWidth, 5)];
        lineView.backgroundColor = self.lineColor;
        [_titleLabel addSubview:lineView];
    }
    return _titleLabel;
}

- (UIView *)padLine{
    if (!_padLine) {
        _padLine = [[UIView alloc]initWithFrame:CGRectZero];
        _padLine.height = self.padHeight;
        _padLine.backgroundColor = self.padColor;
    }
    return _padLine;
}
@end
